﻿/**********************************************
 * 功能名称：成本归集点数据汇总查询
编写人：  杨莹
建立时间： 杨 2009年3月4日
 *修改时间： 杨莹 2009年3月520日
说明：
 * 列表显示所有作业中心的资源。
 * 包括人工，设备、厂房等。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.Resource
{
    public partial class ResourceCheckReport : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());


            }

            
        }

        private DataTable ConvertToDataTable(string[,] arr)
        {

            DataTable dataSouce = new DataTable();
            //for (int i = 0; i < arr.GetLength(1); i++)
            //{
            DataColumn newColumn1 = new DataColumn("作业中心ID", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn1);

            DataColumn newColumn2 = new DataColumn("作业中心名称", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn2);

            DataColumn newColumn3 = new DataColumn("人工", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn3);

            DataColumn newColumn4 = new DataColumn("设备", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn4);

            DataColumn newColumn5 = new DataColumn("厂房", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn5);

            DataColumn newColumn6 = new DataColumn("闲置", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn6);

            DataColumn newColumn7 = new DataColumn("协调", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn7);

            DataColumn newColumn8 = new DataColumn("水费", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn8);

            DataColumn newColumn9 = new DataColumn("电费", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn9);

            DataColumn newColumn10 = new DataColumn("蒸汽", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn10);

            DataColumn newColumn11= new DataColumn("合计", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn11);
            //}
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                DataRow newRow = dataSouce.NewRow();
                newRow[0] = arr[i, 0];
                newRow[1] = arr[i, 1];
                newRow[2] = arr[i, 2];
                newRow[3] = arr[i, 3];
                newRow[4] = arr[i, 4];
                newRow[5] = arr[i, 5];
                newRow[6] = arr[i, 6];
                newRow[7] = arr[i, 7];
                newRow[8] = arr[i, 8];
                newRow[9] = arr[i, 9];
                newRow[10] = arr[i, 10];

                dataSouce.Rows.Add(newRow);
            }
            return dataSouce;

        }

        //定义全局变量，并对列数据绑定事件进行编程。以显示汇总数据
        decimal total_RG = 0;
        decimal total_SB = 0;
        decimal total_CF = 0;
        decimal total_XZ = 0;
        decimal total_ZZ = 0;
        decimal total_SF = 0;
        decimal total_DF = 0;
        decimal total_ZQ = 0;
        decimal total = 0;


        protected void GridView_ResourceReportCheck_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // decimal totalcash = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                total_RG += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "人工"));
                total_SB += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "设备"));                  
                total_CF += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "厂房"));                     
                total_XZ += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "闲置"));                          
                    total_ZZ += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "协调"));                           
                    total_SF += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "水费"));                             
                    total_DF += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "电费"));                          
                    total_ZQ += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "蒸汽"));                              
                    total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "合计"));
                    
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "合计";
                e.Row.Cells[2].Text = String.Format("{0:N2}",total_RG);
                e.Row.Cells[3].Text = String.Format("{0:N2}",total_SB);
                e.Row.Cells[4].Text = String.Format("{0:N2}",total_CF);
                e.Row.Cells[5].Text = String.Format("{0:N2}",total_XZ);
                e.Row.Cells[6].Text = String.Format("{0:N2}",total_ZZ);
                e.Row.Cells[7].Text = String.Format("{0:N2}",total_SF);
                e.Row.Cells[8].Text = String.Format("{0:N2}",total_DF);
                e.Row.Cells[9].Text = String.Format("{0:N2}",total_ZQ);
                e.Row.Cells[10].Text = String.Format("{0:N2}",total);


            }
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string MyQueryAccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;
  

            string CurrentUser = Session[ServerVars.sysSessionUserName].ToString();

            var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == MyQueryAccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
                Response.Write("<Script>alert('会计期间不存在，请检查输入是否正确')</Script>");
                return;
            }
            else if (IsValidString.Count() == 1)
            {

                var myQueryData = (from a in db.TABC_JC_CostCenterItems
                                   join d in db.TABC_JS_CostCenter on a.作业中心ID equals d.作业中心ID
                                   //  into temps
                                   where a.会计期间ID == MyQueryAccountPeriod
                                   orderby d.作业中心名称, a.成本细项ID
                                   // from temp in temps.DefaultIfEmpty()
                                   select new
                                   {
                                       a.作业中心ID,
                                       a.成本细项ID,
                                       a.金额,
                                       d.作业中心名称
                                   }
                                  ).ToArray();

                var myCenters = (from b in myQueryData
                                 orderby b.作业中心名称
                                 select b.作业中心ID).Distinct();
                if (myCenters.Count() == 0)
                {
                    Response.Write("<Script>alert('无当前会计期间成本信息')</Script>");
                    return;
                }

                string[,] MyGridViewList = new string[myCenters.Count(), 11];

                int i = 0;
                #region 插入数据的循环
                foreach (var myCenter in myCenters)
                {
                    var myRows = from c in myQueryData
                                 where c.作业中心ID == myCenter
                                 select c;

                    decimal sum = 0;

                    MyGridViewList[i, 2] = "0";
                    MyGridViewList[i, 3] = "0";
                    MyGridViewList[i, 4] = "0";
                    MyGridViewList[i, 5] = "0";
                    MyGridViewList[i, 6] = "0";
                    MyGridViewList[i, 7] = "0";
                    MyGridViewList[i, 8] = "0";
                    MyGridViewList[i, 9] = "0";
                    MyGridViewList[i, 10] = "0";

                    foreach (var myRow in myRows)
                    {
                        MyGridViewList[i, 0] = myRow.作业中心ID.ToString();
                        MyGridViewList[i, 1] = myRow.作业中心名称;





                        sum = sum + myRow.金额;

                        switch (myRow.成本细项ID)
                        {
                            case "RG":
                                {
                                    MyGridViewList[i, 2] = String.Format("{0:N2}",myRow.金额);
                                    break;
                                }


                            case "SB":
                                {
                                    MyGridViewList[i, 3] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }

                            case "CF":
                                {
                                    MyGridViewList[i, 4] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }

                            case "XZ":
                                {
                                    MyGridViewList[i, 5] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }

                            case "ZZ":
                                {
                                    MyGridViewList[i, 6] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }

                            case "SF":
                                {
                                    MyGridViewList[i, 7] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }

                            case "DF":
                                {
                                    MyGridViewList[i, 8] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }
                            case "ZQ":
                                {
                                    MyGridViewList[i, 9] = String.Format("{0:N2}", myRow.金额);
                                    break;
                                }
                        }

                        MyGridViewList[i, 10] = String.Format("{0:N2}",sum); 



                    }
                    i++;

                }

                #endregion


                GridView_ResourceReportCheck.DataSource = ConvertToDataTable(MyGridViewList);
                GridView_ResourceReportCheck.DataBind();
            }
        }




    }
}
